package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import android.widget.FrameLayout
import com.af.community.smart.base.BaseActivity
import com.af.community.smart.base.addItems
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.dp2px
import com.af.community.smart.utils.subscribeByFinally
import com.af.community.smart.viewmodel.HouseViewModel
import com.library.flowlayout.FlowLayoutManager
import com.library.flowlayout.SpaceItemDecoration
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.jetbrains.anko.startActivity
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HouseSelectActivity : BaseActivity() {

    private val list = ArrayList<CommonData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        initTitle(title = "房屋类型")

        getViewModel().getGridHouse()
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    list.addItems(it)
                    mAdapter.updateData(list)
                }
            )
    }

    override fun getViewModel() = getViewModel<HouseViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        swipe_refresh.isEnabled = false
        recycle_list.apply {
            layoutParams = FrameLayout.LayoutParams(layoutParams).apply {
                setMargins(dp2px(7f), dp2px(10f), dp2px(7f), dp2px(10f))
            }

            layoutManager = FlowLayoutManager()
            addItemDecoration(
                SpaceItemDecoration(
                    dp2px(10f),
                    dp2px(7f)
                )
            )
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_select_list) { data, injector ->
                injector.text(R.id.item_select, data.dictLabel)
                    .clicked(R.id.item_select) {
                        startActivity<HouseAddActivity>(
                            "label" to data.dictLabel,
                            "value" to data.dictValue
                        )
                    }
            }
            .attachTo(recycle_list)
    }

}
